{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import chatglm_cpp\n",
    "from PIL import Image\n",
    "from tool_register import get_tools, run_function\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipeline = chatglm_cpp.Pipeline(\"../chatglm3-ggml.bin\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "TOOLS = [\n",
    "    {\n",
    "        \"name\": \"random_number_generator\",\n",
    "        \"description\": \"Generates a random number x, s.t. range[0] <= x < range[1]\",\n",
    "        \"parameters\": {\n",
    "            \"type\": \"object\",\n",
    "            \"properties\": {\n",
    "                \"seed\": {\"description\": \"The random seed used by the generator\", \"type\": \"integer\"},\n",
    "                \"range\": {\n",
    "                    \"description\": \"The range of the generated numbers\",\n",
    "                    \"type\": \"array\",\n",
    "                    \"items\": [{\"type\": \"integer\"}, {\"type\": \"integer\"}],\n",
    "                },\n",
    "            },\n",
    "            \"required\": [\"seed\", \"range\"],\n",
    "        },\n",
    "    },\n",
    "    {\n",
    "        \"name\": \"get_weather\",\n",
    "        \"description\": \"Get the current weather for `city_name`\",\n",
    "        \"parameters\": {\n",
    "            \"type\": \"object\",\n",
    "            \"properties\": {\"city_name\": {\"description\": \"The name of the city to be queried\", \"type\": \"string\"}},\n",
    "            \"required\": [\"city_name\"],\n",
    "        },\n",
    "    },\n",
    "]\n",
    "\n",
    "TOOL_SYSTEM_PROMPT = (\n",
    "    \"Answer the following questions as best as you can. You have access to the following tools:\\n\"\n",
    "    + json.dumps(TOOLS, indent=4)\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "messages = [chatglm_cpp.ChatMessage(role='system', content=TOOL_SYSTEM_PROMPT)\n",
    "            , chatglm_cpp.ChatMessage(role='user', content=\"今天北京的天气怎么样？\")]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "chatMessage = pipeline.chat(\n",
    "                messages,\n",
    "                max_length=2048,\n",
    "                max_context_length=2048,\n",
    "                do_sample=0.95,\n",
    "                top_k=0,\n",
    "                top_p=0.8,\n",
    "                temperature=0.8,\n",
    "                repetition_penalty=1.0,\n",
    "                num_threads=0,\n",
    "                stream=False,\n",
    "            )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatMessage(role=\"assistant\", content=\"```python\n",
       "tool_call(city_name='北京')\n",
       "```\", tool_calls=[ToolCallMessage(type=\"function\", function=FunctionMessage(name=\"get_weather\", arguments=\"tool_call(city_name='北京')\"), code=CodeMessage(input=\"\"))])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chatMessage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "(tool_call,) = chatMessage.tool_calls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ToolCallMessage(type=\"function\", function=FunctionMessage(name=\"get_weather\", arguments=\"tool_call(city_name='北京')\"), code=CodeMessage(input=\"\"))"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tool_call"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "observation = run_function(tool_call.function.name, tool_call.function.arguments)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"current_condition\": {\"temp_C\": \"0\", \"FeelsLikeC\": \"-1\", \"humidity\": \"43\", \"weatherDesc\": [{\"value\": \"Sunny\"}], \"observation_time\": \"05:14 AM\"}}'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "observation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "OBSERVATION_MAX_LENGTH = 1024\n",
    "if isinstance(observation, str) and len(observation) > OBSERVATION_MAX_LENGTH:\n",
    "    observation = observation[:OBSERVATION_MAX_LENGTH] + \" [TRUNCATED]\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"current_condition\": {\"temp_C\": \"0\", \"FeelsLikeC\": \"-1\", \"humidity\": \"43\", \"weatherDesc\": [{\"value\": \"Sunny\"}], \"observation_time\": \"05:14 AM\"}}'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "observation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[ChatMessage(role=\"system\", content=\"Answer the following questions as best as you can. You have access to the following tools:\n",
       " [\n",
       "     {\n",
       "         \\\"name\\\": \\\"random_number_generator\\\",\n",
       "         \\\"description\\\": \\\"Generates a random number x, s.t. range[0] <= x < range[1]\\\",\n",
       "         \\\"parameters\\\": {\n",
       "             \\\"type\\\": \\\"object\\\",\n",
       "             \\\"properties\\\": {\n",
       "                 \\\"seed\\\": {\n",
       "                     \\\"description\\\": \\\"The random seed used by the generator\\\",\n",
       "                     \\\"type\\\": \\\"integer\\\"\n",
       "                 },\n",
       "                 \\\"range\\\": {\n",
       "                     \\\"description\\\": \\\"The range of the generated numbers\\\",\n",
       "                     \\\"type\\\": \\\"array\\\",\n",
       "                     \\\"items\\\": [\n",
       "                         {\n",
       "                             \\\"type\\\": \\\"integer\\\"\n",
       "                         },\n",
       "                         {\n",
       "                             \\\"type\\\": \\\"integer\\\"\n",
       "                         }\n",
       "                     ]\n",
       "                 }\n",
       "             },\n",
       "             \\\"required\\\": [\n",
       "                 \\\"seed\\\",\n",
       "                 \\\"range\\\"\n",
       "             ]\n",
       "         }\n",
       "     },\n",
       "     {\n",
       "         \\\"name\\\": \\\"get_weather\\\",\n",
       "         \\\"description\\\": \\\"Get the current weather for `city_name`\\\",\n",
       "         \\\"parameters\\\": {\n",
       "             \\\"type\\\": \\\"object\\\",\n",
       "             \\\"properties\\\": {\n",
       "                 \\\"city_name\\\": {\n",
       "                     \\\"description\\\": \\\"The name of the city to be queried\\\",\n",
       "                     \\\"type\\\": \\\"string\\\"\n",
       "                 }\n",
       "             },\n",
       "             \\\"required\\\": [\n",
       "                 \\\"city_name\\\"\n",
       "             ]\n",
       "         }\n",
       "     }\n",
       " ]\", tool_calls=[]),\n",
       " ChatMessage(role=\"user\", content=\"今天北京的天气怎么样？\", tool_calls=[]),\n",
       " ChatMessage(role=\"observation\", content=\"{\\\"current_condition\\\": {\\\"temp_C\\\": \\\"0\\\", \\\"FeelsLikeC\\\": \\\"-1\\\", \\\"humidity\\\": \\\"43\\\", \\\"weatherDesc\\\": [{\\\"value\\\": \\\"Sunny\\\"}], \\\"observation_time\\\": \\\"05:14 AM\\\"}}\", tool_calls=[])]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "messages.append(chatglm_cpp.ChatMessage(role=\"observation\", content=observation))\n",
    "messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "chatMessage = pipeline.chat(\n",
    "                messages,\n",
    "                max_length=2048,\n",
    "                max_context_length=2048,\n",
    "                do_sample=0.95,\n",
    "                top_k=0,\n",
    "                top_p=0.8,\n",
    "                temperature=0.8,\n",
    "                repetition_penalty=1.0,\n",
    "                num_threads=0,\n",
    "                stream=False,\n",
    "            )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatMessage(role=\"assistant\", content=\"根据最新的数据，北京今天的天气情况如下：当前气温为0℃，体感温度为-1℃，相对湿度为43%，天气状况为晴朗。\", tool_calls=[])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chatMessage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chatglm3-demo",
   "language": "python",
   "name": "chatglm3-demo"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
